package fragment.classicfragments;

import android.view.View;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.horo.myapplication.R;

import java.util.List;

import entity.RecommendDetailsEntity;
import fragment.AbsBaseFragment;
import tools.MarqueeTextView;
import utils.adapter.RecomendListAdpter;
import volley.VolleySingleton;

/**
 * Created by dllo on 15/11/28.
 */
public class ClassifDetalsFragment extends AbsBaseFragment implements View.OnClickListener {
    private TextView textDp;
    private RecomendListAdpter listAdpter;
    private String title;
    private LinearLayout stuck_action;
    private ListView lv;
    private LinearLayout inivs, tv;
    private RelativeLayout heardRelat;

    private TextView albun_collection, album_down, recomemd;
    long aid;
    View heard;//定义一个头布局

    private ImageView recomedBack,share;
    private MarqueeTextView recomTitle;
    private NetworkImageView headrIv, smallIv, recomendBigImg;
    private TextView renickName, recomdplaytimes;
    @Override
    protected int getLayoutView() {
        return R.layout.fragment_details_recommend;
    }

    public  ClassifDetalsFragment (long aid ){
        this.aid = aid;
    }
    @Override
    protected void initView() {
        recomedBack = findView(R.id.recomedBack);

        renickName = findView(R.id.renickName);
        recomendBigImg = findView(R.id.recomendBigImg);
        smallIv = findView(R.id.smallIv);
        recomTitle = findView(R.id.recomTitle);
        recomdplaytimes = findView(R.id.recomdplaytimes);
        inivs = findView(R.id.invis);
        lv = findView(R.id.lv);
//        recomedBack.setOnClickListener(this);

    }

    @Override
    protected void initData() {
        //悬浮部分
        View action = View.inflate(getContext(), R.layout.recommend_stuck_action, null);
        //头部内容
        heard = View.inflate(getContext(), R.layout.recommend_stick_heard, null);
        //初始化头布局的空间
        heard.findViewById(R.id.recomedBack).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goBack();
            }
        });
        heard.findViewById(R.id.share).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {//分享的点击
                Toast.makeText(getContext(), "分享到朋友圈", Toast.LENGTH_SHORT).show();
            }
        });
        lv.addHeaderView(heard);
        lv.addHeaderView(action);
        lv.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (firstVisibleItem >= 1) {
                    //悬浮部分
                    inivs.setVisibility(View.VISIBLE);
                } else {
                    inivs.setVisibility(View.GONE);

                }
            }
        });
        initNetwork();
        listAdpter = new RecomendListAdpter();
        lv.setAdapter(listAdpter);

    }

    /**
     * http://mobile.ximalaya.com/mobile/others/ca/album/track/
     * 239463/true/1/20?albumId=239463
     * &isAsc=true&device=android&pageSize=20

     */
    private void initNetwork() {
        String URL = "http://mobile.ximalaya.com/mobile/others/ca/album/track/" +
                aid + "/true/1/20?"+ "&albumId=" + aid +
                "&isAsc=true&device=android&" +"&pageSize=20";

        VolleySingleton.getInstance().addRequest(URL,
                RecommendDetailsEntity.class, new Response.Listener<RecommendDetailsEntity>() {
                    @Override
                    public void onResponse(RecommendDetailsEntity response) {
                        if (response != null) {
                            RecommendDetailsEntity.AlbumEntity album = response.getAlbum();
                            if (album != null) {
                                double firstPlaysCounts = ((double) album.getPlayTimes() / 10000);
                                String firstPC = String.format("%1$.2f", firstPlaysCounts);

                                ((TextView) heard.findViewById(R.id.recomdplaytimes)).setText(firstPC + "万");
                                ((TextView) heard.findViewById(R.id.renickName)).setText(album.getNickname());
                                ((TextView) heard.findViewById(R.id.recomTitle)).setText(album.getTitle());
                                VolleySingleton.getInstance().getBitmap(album.getCoverSmall(),
                                        ((NetworkImageView) heard.findViewById(R.id.smallIv)));
                                VolleySingleton.getInstance().getBitmap(album.getCoverMiddle(),
                                        ((NetworkImageView) heard.findViewById(R.id.recomendBigImg)));
                            }

                            if (response.getTracks() != null) {
                                List<RecommendDetailsEntity.TracksEntity.ListEntity> listEntity
                                        = response.getTracks().getList();
                                if (listEntity != null) {
                                    listAdpter.addData(listEntity);
                                }
                            }


                            dismissDialog();
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        dismissDialog();
                        toastError();
                    }
                });
        showDialog();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.recomedBack:
                goBack();
                break;
        }
    }
}
